﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
namespace SoonCode.Core
{
    public class DeviceHelper
    {
        public static string GetHardwareId()
        {
            string cpuId = GetProcessorId();
            string diskId = GetDiskId();
            string macAddress = GetMacAddress();

            // 组合这些信息来生成一个唯一标识码
            string hardwareId = $"{cpuId}_{diskId}_{macAddress}";

            return hardwareId;
        }

        public static string GetProcessorId()
        {
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT ProcessorId FROM Win32_Processor"))
            {
                ManagementObjectCollection collection = searcher.Get();
                string processorId = collection.Cast<ManagementBaseObject>().First()["ProcessorId"].ToString();
                return processorId;
            }
        }

        public static string GetDiskId()
        {
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT SerialNumber FROM Win32_DiskDrive"))
            {
                ManagementObjectCollection collection = searcher.Get();
                string diskId = collection.Cast<ManagementBaseObject>().First()["SerialNumber"].ToString();
                return diskId;
            }
        }

        public static string GetMacAddress()
        {
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT MACAddress FROM Win32_NetworkAdapter WHERE PhysicalAdapter=True"))
            {
                ManagementObjectCollection collection = searcher.Get();
                string macAddress = collection.Cast<ManagementBaseObject>().First()["MACAddress"].ToString();
                return macAddress;
            }
        }
    }
}
